package pl.edu.amu.wmi.kino.visualjavafx.model.objects.shapes;

import pl.edu.amu.wmi.kino.visualjavafx.model.objects.*;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.path.Path;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
import java.util.Iterator;
// #[regen=yes,id=DCE.CBB94F94-BDFA-D37E-D16F-154B743C9188]
import pl.edu.amu.wmi.kino.visualjavafx.model.animation.animationTime.Timeline;
// </editor-fold> 
public class Polyline implements Shape, java.io.Serializable {

    // Code block for changeListeners support start
    protected PropertyChangeSupport propertySupport= new PropertyChangeSupport(this);
    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    // Code block for changeListeners support end

    private double hPos = 0;
    private double vPos = 0;
    private double rotation = 0;
    private double vCenterDelta = 0;
    private double hCenterDelta = 0;
    private double vSize = 20;
    private double hSize = 10;
    private double borderWidth = 1;
    private double opacity = 1;
    private double hShear = 0;
    private double vShear = 0;
    private Color borderColor = new Color(0, 0, 0);
    private Color fillColor = new Color(255, 255, 255);
    private boolean visible = true;
    private Timeline timeline = new Timeline();

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.172F8509-76E7-9FC1-5350-6A6F869328F6]
    // </editor-fold> 
    private ArrayList<Point> mPoint = new ArrayList<Point>();

    

    /**
     * This method is used to calculate bounding rectangle - top-left of it is 0,0 and
     * all points coordinates are relative to it
     * 
     */
    public void addPoint(Point p){
        this.mPoint.add(p);
        this.normalize();
    }
    private void normalize(){
        double minX=Double.POSITIVE_INFINITY;
        double minY=Double.POSITIVE_INFINITY;
        double maxX=Double.NEGATIVE_INFINITY;
        double maxY=Double.NEGATIVE_INFINITY;
        if(mPoint.size()<=1) return;
        for(Point p : mPoint){
            if(p.getHPos()<minX)
                minX=p.getHPos();
            if(p.getVPos()<minY)
                minY=p.getVPos();
            if(p.getHPos()>maxX)
                maxX=p.getHPos();
            if(p.getVPos()>maxY)
                maxY=p.getVPos();
        }
        this.setHPos(minX);
        this.setVPos(minY);
        maxX=maxX-minX;
        maxY=maxY-minY;
        this.hSize=maxX;
        this.vSize=maxY;
        
        for(Point p : mPoint){
            p.setHPos(p.getHPos()-minX);
            p.setVPos(p.getVPos()-minY);
        }



    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.C6373731-3FD5-6060-6A5D-4F91012E5A1D]
    // </editor-fold> 
    public Polyline() {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.FEC077FF-8A94-33D7-83EC-208FC21CA20B]
    // </editor-fold> 
    @Override
    public double getHPos() {
        return hPos;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0F8B933B-71CE-3B04-6DC7-C022CBF008A7]
    // </editor-fold> 
    @Override
    public void setHPos(double val) {
        double oldVal = this.hPos;
        this.hPos=val;
        propertySupport.firePropertyChange( "hPos" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CCD3FA88-7F00-C31B-F356-1CC7A7921B93]
    // </editor-fold> 
    @Override
    public double getVPos() {
        return vPos;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.DF6EA8EB-7593-F924-A3D0-329E8E1B1A93]
    // </editor-fold> 
    @Override
    public void setVPos(double val) {
        double oldVal = this.vPos;
        this.vPos=val;
        propertySupport.firePropertyChange( "vPos" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4BA4F3EF-C505-2CE8-BC4E-0F3A471B64B9]
    // </editor-fold> 
    @Override
    public double getRotation() {
        return rotation;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AE9390A4-1403-3BFC-E827-90339EAB1447]
    // </editor-fold> 
    @Override
    public void setRotation(double val) {
        double oldVal = this.rotation;
        this.rotation=val;
        propertySupport.firePropertyChange( "rotation" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.8835222B-F5A9-D9AF-C990-4EC2F613711D]
    // </editor-fold> 
    @Override
    public double getVCenterDelta() {
        return this.vCenterDelta;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.ED99B6DE-9C96-D896-8DD3-4DEA47D5EA90]
    // </editor-fold> 
    @Override
    public void setVCenterDelta(double val) {
        double oldVal = this.vCenterDelta;
        this.vCenterDelta=val;
        propertySupport.firePropertyChange( "vCenterDelta" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.66CB3780-8567-DC69-1E9D-60B02628A3AD]
    // </editor-fold> 
    @Override
    public double getHCenterDelta() {
        return this.hCenterDelta;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2D4D5B9A-3AF0-6614-114F-7C3977DC0837]
    // </editor-fold> 
    @Override
    public void setHCenterDelta(double val) {
        double oldVal = this.hCenterDelta;
        this.hCenterDelta=val;
        propertySupport.firePropertyChange( "hCenterDelta" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.231B9997-28EC-F6B8-4BCC-87A04C17ECCA]
    // </editor-fold> 
    @Override
    public double getSizeH() {
        return this.hSize;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1352742F-4B8E-CEA8-E00A-31B24863CB87]
    // </editor-fold> 
    @Override
    public void setSizeH(double val) {
        double oldVal = this.hSize;
        this.hSize=val;
        propertySupport.firePropertyChange( "hSize" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1EB9F52B-D048-6CC5-0377-F074603807DC]
    // </editor-fold> 
    @Override
    public double getSizeV() {
        return this.vSize;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CB262957-73CA-3E8E-7153-522BC475FD9D]
    // </editor-fold> 
    @Override
    public void setSizeV(double val) {
        double oldVal = this.vSize;
        this.vSize=val;
        propertySupport.firePropertyChange( "vSize" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2754187F-2FED-88B5-82E4-86F1C95523AC]
    // </editor-fold> 
    @Override
    public double getBorderWidth() {
        return this.borderWidth;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1C1CA547-8CE9-9844-8D82-9EDB35679F92]
    // </editor-fold> 
    @Override
    public void setBorderWidth(double val) {
        double oldVal = this.borderWidth;
        this.borderWidth=val;
        propertySupport.firePropertyChange( "borderWidth" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.65026A34-8722-76A7-9423-36C47ED548BE]
    // </editor-fold> 
    @Override
    public double getOpacity() {
        return this.opacity;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.54A58260-2377-B118-707D-6575DD512FFF]
    // </editor-fold> 
    @Override
    public void setOpacity(double val) {
        double oldVal = this.opacity;
        this.opacity=val;
        propertySupport.firePropertyChange( "opacity" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.653206E0-9210-36C7-750E-2888FBAF6048]
    // </editor-fold> 
    public ArrayList<Point> getPoint() {
        return mPoint;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.78010D7A-A017-D198-8C3C-EBF26B952CD6]
    // </editor-fold> 
    @Override
    public double getHShear() {
        return this.hShear;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.F82CA5F5-ED7C-0E51-51FF-0B7135ED3727]
    // </editor-fold> 
    @Override
    public void setHShear(double val) {
        double oldVal = this.hShear;
        this.hShear=val;
        propertySupport.firePropertyChange( "hShear" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.BB548EF7-DD10-54EE-2C76-A7F6D154BED6]
    // </editor-fold> 
    @Override
    public double getVShear() {
        return this.vShear;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B1CB45FD-FAC3-C70E-E46A-53A399EBF110]
    // </editor-fold> 
    @Override
    public void setVShear(double val) {
        double oldVal = this.vShear;
        this.vShear=val;
        propertySupport.firePropertyChange( "vShear" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2FFC0DAA-7226-8AD8-A29D-EADEC96F747F]
    // </editor-fold> 
    @Override
    public Path toPath() {
        return null;
    }

    public Polygon close() {
        Polygon x = new Polygon();

        for (Iterator<Point> it = mPoint.iterator(); it.hasNext();) {
            Point point = it.next();
            x.getPoint().add(point);
        }
        x.setBorderColor(this.borderColor);
        x.setBorderWidth(this.borderWidth);
        x.setVCenterDelta(this.vCenterDelta);
        x.setFillColor(fillColor);
        x.setHCenterDelta(this.hCenterDelta);
        x.setHPos(hPos);
        x.setHShear(this.hShear);
        x.setOpacity(this.opacity);
        x.setRotation(this.rotation);
        x.setSizeH(hSize);
        x.setSizeV(vSize);
        x.setVPos(vPos);
        x.setVShear(this.vShear);
        x.setVisibility(visible);

        return x;
    }


    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EC5C90D3-AAFC-399B-1BC8-5496561BF88B]
    // </editor-fold> 
    @Override
    public Color getBorderColor() {
        return this.borderColor;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E7C8762B-79C6-C34E-5EB4-3ED100ED7EA9]
    // </editor-fold> 
    @Override
    public void setBorderColor(Color val) {
        Color oldVal = this.borderColor;
        this.borderColor=val;
        propertySupport.firePropertyChange( "borderColor" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.8D8B8363-5D17-0338-DA33-FBE4A1B17F8A]
    // </editor-fold> 
    @Override
    public Color getFillColor() {
        return this.fillColor;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.47F4F5CE-8917-ADF8-7295-6BAF6A634777]
    // </editor-fold> 
    @Override
    public void setFillColor(Color val) {
        Color oldVal = this.fillColor;
        this.fillColor=val;
        propertySupport.firePropertyChange( "fillColor" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.5BC561A4-94FD-D127-1316-AFF982C2ECA7]
    // </editor-fold> 
    @Override
    public void setVisibility(boolean val) {
        boolean oldVal = this.visible;
        this.visible=val;
        propertySupport.firePropertyChange( "visible" , oldVal, val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D9C7CA13-CF8E-350B-7155-94AFB619550B]
    // </editor-fold> 
    @Override
    public boolean getVisibility() {
        return this.visible;
    }

    @Override
    public Timeline getTimeline() {
        return this.timeline;
    }

    @Override
    public VisualObject shallowClone(){
        Polyline res = new Polyline();
        res.hPos=this.hPos;
        res.vPos=this.vPos;
        res.rotation=this.rotation;
        res.vCenterDelta=this.vCenterDelta;
        res.hCenterDelta=this.hCenterDelta;
        res.vSize=this.vSize;
        res.hSize=this.hSize;
        res.opacity=this.opacity;
        res.hShear=this.hShear;
        res.vShear=this.vShear;
        res.borderColor=new Color(borderColor.getRGB());
        res.fillColor=new Color(fillColor.getRGB());
        res.visible=this.visible;
        res.mPoint=new ArrayList<Point>();
        for(Point p : this.mPoint){
            res.mPoint.add(p.clone());
        }
        return res;
    }
}

